package com.work.demo.entity;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @ProjectName: demo
 * @Package: com.example.demo.entity
 * @ClassName: Test
 * @Author: Deng YuBin
 * @Description: ds
 * @Date: 2020/10/14 18:12
 * @Version: 1.0
 */
@Slf4j
public class Test {

	private ExecutorService cockpitHealthCheckExecutor() {
		return new ThreadPoolExecutor(6, 12, 60, TimeUnit.SECONDS,
						new LinkedBlockingQueue<>(1024),
						new ThreadFactoryBuilder().setNamePrefix("cockpitHealthCheckExecutor-%d").build(),
						(r, executor) -> r.run()
		);
	}

	public static void main(String[] args) {
		ConcurrentHashMap<String, List<Future>> stringFutureMap = new ConcurrentHashMap<>(16);
		Test test = new Test();
		ExecutorService executorService = test.cockpitHealthCheckExecutor();
		for (int i = 0; i < 100; i++) {
			List<Future> futureList = new ArrayList<>();
			String ipAndPort = i + "";
			stringFutureMap.put(ipAndPort, futureList);
			// 检查健康
			futureList.add(executorService.submit(() -> "异常"));
			futureList.add(executorService.submit(() -> "正常"));
		}
		int i = 0;
		for (Map.Entry<String, List<Future>> s : stringFutureMap.entrySet()) {
			List<Future> value = s.getValue();
			System.out.println(i + JSON.toJSONString(value));
			i++;
		}
	}
}
